<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - LAgain</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .login-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
            position: relative;
            overflow: hidden;
        }
        
        .login-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
        }
        
        .logo {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .logo h1 {
            color: #333;
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 8px;
        }
        
        .logo p {
            color: #666;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            background: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .form-group input.error {
            border-color: #e74c3c;
            background: #fdf2f2;
        }
        
        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }
        
        .login-btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .login-btn:active {
            transform: translateY(0);
        }
        
        .login-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .loading {
            display: none;
            text-align: center;
            margin-top: 20px;
        }
        
        .loading::after {
            content: '';
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 2px solid #f3f3f3;
            border-top: 2px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .alert {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
        }
        
        .alert.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .role-info {
            text-align: center;
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 10px;
            font-size: 14px;
            color: #666;
        }
        
        .role-info strong {
            color: #333;
        }
        
        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
                margin: 10px;
            }
            
            .logo h1 {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <h1>LAgain</h1>
            <p>欢迎回来，请登录您的账号</p>
        </div>
        
        <div id="alert" class="alert"></div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="phone">手机号</label>
                <input type="tel" id="phone" name="phone" placeholder="请输入手机号" required>
                <div class="error-message" id="phoneError">请输入正确的手机号</div>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
                <div class="error-message" id="passwordError">密码不能为空</div>
            </div>
            
            <button type="submit" class="login-btn" id="loginBtn">登录</button>
        </form>
        
        <div class="loading" id="loading">登录中...</div>
        
        <div class="role-info">
            <strong>测试账号：</strong><br>
            管理员：18947924869 / zhaoxingyu123321<br>
            普通用户：18748420124 / 123456
        </div>
    </div>
    
    <script>
        // 表单验证
        function validatePhone(phone) {
            const phoneRegex = /^1[3-9]\d{9}$/;
            return phoneRegex.test(phone);
        }
        
        function validatePassword(password) {
            return password && password.length > 0;
        }
        
        function showError(inputId, errorId, message) {
            const input = document.getElementById(inputId);
            const error = document.getElementById(errorId);
            input.classList.add('error');
            error.textContent = message;
            error.style.display = 'block';
        }
        
        function hideError(inputId, errorId) {
            const input = document.getElementById(inputId);
            const error = document.getElementById(errorId);
            input.classList.remove('error');
            error.style.display = 'none';
        }
        
        function showAlert(message, type = 'error') {
            const alert = document.getElementById('alert');
            alert.textContent = message;
            alert.className = `alert ${type}`;
            alert.style.display = 'block';
            
            // 3秒后自动隐藏
            setTimeout(() => {
                alert.style.display = 'none';
            }, 3000);
        }
        
        function setLoading(loading) {
            const loginBtn = document.getElementById('loginBtn');
            const loadingDiv = document.getElementById('loading');
            
            if (loading) {
                loginBtn.disabled = true;
                loginBtn.textContent = '登录中...';
                loadingDiv.style.display = 'block';
            } else {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
                loadingDiv.style.display = 'none';
            }
        }
        
        // 登录函数
        async function login(phone, password) {
            try {
                const response = await fetch('/api/phone-auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        phone: phone,
                        password: password
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    // 保存token到localStorage
                    localStorage.setItem('token', result.data.token);
                    localStorage.setItem('user', JSON.stringify(result.data.user));
                    localStorage.setItem('role', result.data.role);
                    localStorage.setItem('roleName', result.data.roleName);
                    
                    showAlert('登录成功！正在跳转...', 'success');
                    
                    // 根据角色跳转到不同页面
                    setTimeout(() => {
                        if (result.data.role === 'ADMIN') {
                            window.location.href = '/admin.html';
                        } else {
                            window.location.href = '/index.html';
                        }
                    }, 1000);
                } else {
                    showAlert(result.message || '登录失败');
                }
            } catch (error) {
                console.error('登录错误:', error);
                showAlert('网络错误，请稍后重试');
            }
        }
        
        // 表单提交事件
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const phone = document.getElementById('phone').value.trim();
            const password = document.getElementById('password').value;
            
            // 清除之前的错误
            hideError('phone', 'phoneError');
            hideError('password', 'passwordError');
            
            let hasError = false;
            
            // 验证手机号
            if (!validatePhone(phone)) {
                showError('phone', 'phoneError', '请输入正确的手机号格式');
                hasError = true;
            }
            
            // 验证密码
            if (!validatePassword(password)) {
                showError('password', 'passwordError', '密码不能为空');
                hasError = true;
            }
            
            if (hasError) {
                return;
            }
            
            // 开始登录
            setLoading(true);
            await login(phone, password);
            setLoading(false);
        });
        
        // 输入框失焦验证
        document.getElementById('phone').addEventListener('blur', function() {
            const phone = this.value.trim();
            if (phone && !validatePhone(phone)) {
                showError('phone', 'phoneError', '请输入正确的手机号格式');
            } else {
                hideError('phone', 'phoneError');
            }
        });
        
        document.getElementById('password').addEventListener('blur', function() {
            const password = this.value;
            if (password && !validatePassword(password)) {
                showError('password', 'passwordError', '密码不能为空');
            } else {
                hideError('password', 'passwordError');
            }
        });
        
        // 检查是否已登录
        window.addEventListener('load', function() {
            const token = localStorage.getItem('token');
            if (token) {
                const role = localStorage.getItem('role');
                if (role === 'ADMIN') {
                    window.location.href = '/admin.html';
                } else {
                    window.location.href = '/index.html';
                }
            }
        });
    </script>
</body>
</html>